import http from"http";
  import sqls from "./sql.mjs";
 
 export default class HttpServers{
    server;
    sql;
    constructor(){
      this.sql= new sqls();
   
        this.server=http.createServer((req,res)=>this.servers(req,res));
     
            }
    async port(port){
        var bool=await this.sql.isConnet()
        if(!bool){
            console.log("数据库连接失败，服务关闭");
            return;
        }
        this.server.listen(port,"localhost",function(){
            console.log("数据库连接成功")
            console.log("服务已打开")
        });
      
    }
  async  servers(req,res){
        var  type=req.url.split("?")[0];
        res.writeHead(200,{
            "Content-Type":"text/html;charset=utf-8",
            "Access-Control-Allow-Origin":"*",
            "Access-Control-Allow-Methods":"*",
        })
       
        
       this. router(type,req,res)
    }
   router(type,req,res){
        switch(type){
            case "/adddata":
            return this.adddata(req,res);
            case "/getdata":
            return this.getgoods(req,res,'productSid');
            case "/changeData":
            return this.changeData(req,res,'classification');
            case "/signUp":
            return this.signUp(req,res);
            case "/signIn":
            return this.signIn(req,res);
            case "/setCar":
            return this.setCar(req,res);
            case "/getCar":
            return this.getCar(req,res); 
            case "/upCar":
            return this.upCar(req,res); 
            case "/searchUser":
             return this.searchUser(req,res); 
             case "/deldata":
             return this.deldata(req,res); 
             case "/getAll":
                return this.getAll(req,res); 
        }
    }
    async getAll(req,res){
        var data=await this.getData(req);   
        try{
            data=JSON.parse(data);
        }catch(e){}; 
        
         var result=await this.sql.getAllData(data,this.sql);
  
         res.end(JSON.stringify({result}));
    }
    async deldata(req,res){
        var data=await this.getData(req);   
        try{
            data=JSON.parse(data);
        }catch(e){}; 
        
         var result=await this.sql.delData(data,this.sql);
    
         res.end(JSON.stringify({result}));
    }
    async searchUser(req,res){
        var data=await this.getData(req);   
    try{
        data=JSON.parse(data);
    }catch(e){}; 
    
     var result=await this.sql.searchUserdata(data,this.sql);

     res.end(JSON.stringify({result}));
    }
    async upCar(req,res){
        var data=await this.getData(req);   
        try{
            data=JSON.parse(data);
        }catch(e){}; ;
      
        var result=await this.sql.updateCar(this.sql);
    
        res.end(JSON.stringify({result}));
    }
   async getCar(req,res){
    var data=await this.getData(req);   
    try{
        data=JSON.parse(data);
    }catch(e){}; 
     var result=await this.sql.getCar(data,this.sql);

     res.end(JSON.stringify({result}));
   }
    async setCar(req,res){
        var data=await this.getData(req);   
        try{
            data=JSON.parse(data);
        }catch(e){}; 
         var result=await this.sql.insertCar(data,this.sql);
         res.end(JSON.stringify({result}));
    }
    async signIn(req,res){
        var data =await  this.getData(req );
        try{data=JSON.parse(data)}catch(e){};
        var result=await this.sql.searchUser(data,this.sql)
        console.log(result);
        res.end(JSON.stringify({result}));
    }
  async  signUp(req,res){
        var data=await this.getData(req);   
        try{
            data=JSON.parse(data);
        }catch(e){}; 
     
         var result=await this.sql.insertUser(data,this.sql);
        
         res.end(JSON.stringify({result}));
    }
  async  changeData(req,res,n){    
        var data=await this.getData(req);    
         var result=await this.sql.selectItem(data, this.sql,n);
         res.end(JSON.stringify({result}));
    }
    getData(req){
        return new Promise((resolve,reject)=>{
            var data='';
            req.on('data',function(chunk){
                data+=chunk
            });
            req.on('end',function(){
                resolve(data)
            })
        })
    }
 async getgoods(req,res,n){
        var data=await this.getData(req);   
        try{
            data=JSON.parse(data);
        }catch(e){}; 
         var result=await this.sql.selectItem(data, this.sql,n);
         res.end(JSON.stringify({result}))
    }
    
   async adddata(req,res){ 
       console.log(req);
        var data=await this.getData(req);   
        try{
            data=JSON.parse(data);
        }catch(e){}; 
         var result=await this.sql.insertDB(data,this.sql);
       console.log(result);
         res.end(JSON.stringify({result}));
    }
}
